package com.gmzg.gmzgoffice.report;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.gmzg.gmzgoffice.R;
import com.gmzg.gmzgoffice.entity.Weekly_report;

import java.util.Collection;
import java.util.List;



/**
 * Created by liuyi_s on 2016/11/10 18:41.
 * Email：liuyi_s@foxmail.com
 */

public class ReportAdapter extends RecyclerView.Adapter<ReportAdapter.ViewHolder> implements View.OnClickListener, View.OnLongClickListener {

    private Context mContext;
    private List<Weekly_report> mReports;
    private onChildClickListener mClickListener;
    private onChildLongClickListener mLongClickListener;
    private RecyclerView mRecyclerView;

    public ReportAdapter(Context context, List<Weekly_report> reports) {
        mContext = context;
        mReports = reports;
    }

    @Override
    public void onAttachedToRecyclerView(RecyclerView recyclerView) {
        super.onAttachedToRecyclerView(recyclerView);
        this.mRecyclerView = recyclerView;
    }

    @Override
    public void onDetachedFromRecyclerView(RecyclerView recyclerView) {
        super.onDetachedFromRecyclerView(recyclerView);
        this.mRecyclerView = null;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater
                .from(mContext)
                .inflate(R.layout.item_report,
                        parent,
                        false);
        view.setOnClickListener(this);
        view.setOnLongClickListener(this);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        Weekly_report report = mReports.get(position);
        holder.mTextViewUserName.setText("发布用户:"+report.getUser_name());
        if (report.getTimeStamp() != null) {
            holder.mTextViewTime.setText("发布时间："+report.getTimeStamp());
        }
        holder.mTextViewFinishWork.setText("本周完成工作：" + report.getFinish_work());
        holder.mTextViewWorkSummary.setText("本周工作总结：" + report.getWork_summary());
        holder.mTextViewNextPlan.setText("下周工作计划：" + report.getNext_plan());
    }

    @Override
    public int getItemCount() {
        return mReports.size();
    }


    @Override
    public void onClick(View v) {
        if (mRecyclerView != null && mClickListener != null) {
            int position = mRecyclerView.getChildAdapterPosition(v);
            mClickListener.onChildclickListener(mRecyclerView,v,position);
//            mLongClickListener.onChildLongClickListener(position);
        }
    }

    @Override
    public boolean onLongClick(View v) {
        if (mRecyclerView != null && mLongClickListener != null) {
            int position = mRecyclerView.getChildAdapterPosition(v);
            mLongClickListener.onChildLongClickListener(position);
        }
        return true;
    }


    public static class ViewHolder extends RecyclerView.ViewHolder {

        private final TextView mTextViewUserName;
        private final TextView mTextViewTime;
        private final TextView mTextViewFinishWork;
        private final TextView mTextViewWorkSummary;
        private final TextView mTextViewNextPlan;
        public ViewHolder(View itemView) {
            super(itemView);
            mTextViewUserName = ((TextView) itemView.findViewById(R.id.item_report_user_name));
            mTextViewTime = ((TextView) itemView.findViewById(R.id.item_report_time));
            mTextViewFinishWork = ((TextView) itemView.findViewById(R.id.item_report_finish_work));
            mTextViewWorkSummary = ((TextView) itemView.findViewById(R.id.item_report_work_summary));
            mTextViewNextPlan = ((TextView) itemView.findViewById(R.id.item_report_next_plan));
        }
    }

    //  add data and refresh the itemView in RecycleView
    public void addAll(Collection<? extends Weekly_report> collection) {
        mReports.addAll(collection);
        notifyDataSetChanged();
    }

    // delete the data
    public void removeAllData() {
        mReports.clear();
        notifyDataSetChanged();
    }

    ///////////////////////////////////////////////////////////////////////////
    // Interface
    ///////////////////////////////////////////////////////////////////////////

    // cause RecycleView has no itemClickListener, create by yourself
    public interface onChildClickListener{
        void onChildclickListener(RecyclerView parent, View view, int position);
    }

    public void onChildChlickListener(onChildClickListener listener) {
        mClickListener = listener;
    }

    public interface onChildLongClickListener{
        void onChildLongClickListener(int position);
    }

    public void onChildLongClickListener(onChildLongClickListener longClickListener) {
        mLongClickListener = longClickListener;
    }

}
